package com.hrms.web.sys_menu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hrms.utils.ResultUtils;
import com.hrms.utils.ResultVo;
import com.hrms.web.sys_menu.entity.AssignTreeParm;
import com.hrms.web.sys_menu.entity.MakeMenuTree;
import com.hrms.web.sys_menu.entity.RouterVO;
import com.hrms.web.sys_menu.entity.SysMenu;
import com.hrms.web.sys_menu.service.SysMenuService;
import com.hrms.web.sys_user.entity.SysUser;
import com.hrms.web.sys_user.service.SysUserService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Title: SysMenuController
 * @Author zhanji
 * @Package com.hrms.web.sys_menu.controller
 * @description:
 */
@RestController
@RequestMapping("/api/menu")
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    @Resource
    private SysUserService sysUserService;

    //新增
    @PostMapping
    public ResultVo add(@RequestBody SysMenu sysMenu) {
        sysMenu.setCreateTime(LocalDateTime.now());
        if (sysMenuService.save(sysMenu)) {
            return ResultUtils.success("新增成功");
        }
        return ResultUtils.error("新增失败");
    }

    //新增
    @PutMapping
    public ResultVo edit(@RequestBody SysMenu sysMenu) {
        sysMenu.setUpdateTime(LocalDateTime.now());
        if (sysMenuService.updateById(sysMenu)) {
            return ResultUtils.success("编辑成功");
        }
        return ResultUtils.error("编辑失败");
    }

    //删除
    @DeleteMapping("/{menuId}")
    public ResultVo delete(@PathVariable("menuId") Long menuId) {
        //如果存在下级菜单，不能删除
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysMenu::getParentId, menuId);
        List<SysMenu> list = sysMenuService.list(queryWrapper);
        if (list.size() > 0) {
            return ResultUtils.error("该菜单存在下级不能删除");
        }
        if (sysMenuService.removeById(menuId)) {
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error("删除失败");
    }

    //列表
    @GetMapping("/getMenuList")
    public ResultVo getMenuList() {
        //排序
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByAsc(SysMenu::getOrderNum);
        //查询所有菜单
        List<SysMenu> list = sysMenuService.list(queryWrapper);
        //组装树数据
        List<SysMenu> menuList = MakeMenuTree.makeTree(list, 0L);
        return ResultUtils.success("查询成功",menuList);
    }

    //上级菜单
    @GetMapping("/getParent")
    public ResultVo getParent(){
        List<SysMenu> list = sysMenuService.getParent();
        return ResultUtils.success("查询成功",list);
    }

    //获取菜单
    @GetMapping("/getRouterMenuList")
    public  ResultVo getRouterMenuList(Long userId){
        //获取用户信息
        SysUser user = sysUserService.getById(userId);
        //菜单数据
        List<SysMenu> menuList =null;
        //判断是否是超级管理员
        if(StringUtils.isNotEmpty(user.getIsAdmin()) && "1".equals(user.getIsAdmin())){
            QueryWrapper<SysMenu> query = new QueryWrapper<>();
            query.lambda().orderByAsc(SysMenu::getOrderNum);
            menuList =sysMenuService.list(query);
        }else {
            menuList =sysMenuService.selectMenuByUserId(userId);
        }
        //过滤菜单数据,去掉按钮数据
        List<SysMenu> collect = Optional.ofNullable(menuList).orElse(new ArrayList<>())
            .stream()
            .filter(item -> item != null && StringUtils.isNotEmpty(item.getType())&&  !item.getType().equals("2"))
            .collect(Collectors.toList());
        //组装路由数据
        List<RouterVO> router = MakeMenuTree.makeRouter(collect, 0L);
        return ResultUtils.success("查询成功",router);
    }
}
